package com.jxzyk.model.center;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.core.entity.I_List;
import com.core.entity.params.MapParams;
import com.core.entity.params.ReqParams;
import com.core.ui.activity.BaseActivity;
import com.jxzyk.R;
import com.jxzyk.adapter.NoteAdapter;
import com.jxzyk.config.API;
import com.jxzyk.entity.Note;
import com.jxzyk.entity.User;
import com.jxzyk.http.BaseRequest.OnDialogListener;
import com.jxzyk.http.req.NotesRequest;
import com.jxzyk.model.notes.NoteDetailsActivity;
import com.jxzyk.util.UserManger;
import com.jxzyk.view.TitleLayout;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

@ContentView(R.layout.act_notes)
public class MyNoteActivity extends BaseActivity {
	@ViewInject(R.id.title)
	TitleLayout title;
	@ViewInject(R.id.grid_notes)
	GridView gridView;

	private NoteAdapter adapter;
	private String Outline_id;
	private String Outline_name;

	int NOTES_LOAD = 1;

	@Override
	public void initData(Bundle bundle) {
		// TODO Auto-generated method stub
	}

	@Override
	public void initUi() {
		// TODO Auto-generated method stub
		title.setTtile("我的笔记");
		title.setOnLeftListener(R.drawable.ic_chevron_left_white_48dp, new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		adapter = (NoteAdapter) new NoteAdapter().instance(getBaseContext(), list);
		gridView.setAdapter(adapter);
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				Note note = (Note) list.get(arg2);
				Bundle bundle = new Bundle();
				bundle.putSerializable(Note.class.getName(), note);
				// showActivity(NoteDetailsActivity.class, bundle);
				Intent intent = new Intent(MyNoteActivity.this, NoteDetailsActivity.class);
				intent.putExtras(bundle);
				startActivityForResult(intent, NOTES_LOAD);
			}
		});
		try {
			loadList(1);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private List<I_List> list = new ArrayList<>();

	private void loadList(int position) throws Exception {
		String url = "";
		ReqParams params = new MapParams();
		switch (position) {
		case 0:
			url = API.Notes.getByOutline;
			params.put("outline_id", getOutline_id());
			break;
		case 1:
			url = API.Notes.getByUser;
			User user = new UserManger(getBaseContext()).getUser();
			params.put("user_id", user.getUser_id());
			break;
		default:
			break;
		}

		NotesRequest request = new NotesRequest(MyNoteActivity.this, url);
		request.setParams(params);
		list.clear();
		request.setOnDialogListener(new OnDialogListener() {

			@Override
			public void onSuccess(JSONObject json) {
				try {
					JSONArray array = json.getJSONArray("data");
					for (int i = 0; i < array.size(); i++) {
						list.add(new Note().instance(array.getJSONObject(i)));
					}
					adapter.notifyDataSetChanged();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(String msg) {
				adapter.notifyDataSetChanged();
			}
		});
		request.show();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case 1:
			try {
				loadList(1);
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;

		default:
			break;
		}
	}

	public String getOutline_name() {
		return Outline_name;
	}

	public void setOutline_name(String outline_name) {
		Outline_name = outline_name;
	}

	public String getOutline_id() {
		return Outline_id;
	}

	public void setOutline_id(String outline_id) {
		Outline_id = outline_id;
	}
}
